<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/18
 * Time: 16:13
 */

namespace Home\Controller;

use Think\Controller;
use Think\Upload;
class MemberController extends Controller
{
    public function index()
    {
        if (isset($_SESSION['msg']['id'])) {
            $id=$_SESSION['msg']['id'];
            $res=M("register")->where('id='.$id)->find();
            //var_dump($res);die;
            $commissions=$res['commissions'];
            $headimg= $res['headimg'] ? $res['headimg'] : '/Public/images/act_per.png';
			if (strpos($headimg, 'http') === false)
			{
				$headimg= U($headimg, '', false);
			}
            
            //var_dump($headimg);die;
            $commission=$res['commission'];
            $this->assign('headimg',$headimg);
            $this->assign("commissions",$commissions);
            $this->display("index");
        }else{
            $this->display('Login/login');
        }
        
    }
    //我的贷款
    Public function myloan()
    {
        $id=$_GET['id'];

        $loanlist = M("loanborrower")->where("register_id={$id}")->select();

        $this->assign("loanlist",$loanlist);

        $this->display("myloan");
    }
    
 
    //我的二维码
    
    public function qrcode(){
        Vendor('phpqrcode.phpqrcode');
        //生成二维码图片
        $object = new \QRcode();
        $url='http://www.shouce.ren/';//网址或者是文本内容
        $level=3;
        $size=4;
        $errorCorrectionLevel =intval($level) ;//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }
    //贷款详情
    public function loandetail()
    {
        $borrower_id =$_GET['borrower_id'];
        $loandetail = M("loanborrower")->where("borrower_id={$borrower_id}")->find();
        $this->assign("loandetail",$loandetail);
        $this->display('loandetail');
    }
    //实名认证
    public function realname()
    {
        $id=$_GET['id'];
        $this->assign("id",$id);
        $this->display("realname");
    }
    public function register()
    {
        if(IS_POST)
        {
            $id=$_GET['id'];
            $param['register_name'] = I('register_name');
            $param['certificate']=I('certificate');

            $data = M("register");
            $re=$data->where("id={$id}")->data($param)->save();
            if($re)
            {
                $this->redirect("Member/index");
            }else
            {
                 $this->redirect("Member/realname");
            }
        }
    }

    //提现
    public function withdraw(){
        if(IS_AJAX){
            $data['uid']=$_SESSION['msg']['id'];
            $data['money']=I('money');
            $data['time']=date('Y-m-d H:i:s',time());
            $res=M('withdraw')->add($data);
            if($res){
                $this->ajaxReturn('申请提交成功');
            }else{
                $this->ajaxReturn('申请提交失败');
            }
            //var_dump($data['time']);die;
        }
    }
    
      //下拉刷新
    public function record()
    {   
        if(isset($_SESSION['msg']['id'])){
            $id=$_SESSION['msg']['id'];
            //$data = M("register")->where("agent_id='{$id}' or upper_agent_id='{$id}' or upper_upper_agent_id='{$id}'")->select();
            $data = M("withdraw")->join('register on withdraw.uid=register.id')->where("uid=".$id)->select();
            //var_dump($data);die;
            $this->display("record");
        }
        /*elseif(IS_AJAX){
            $id=I('id');
            //var_dump($id);die;
            $data = M("withdraw")->where("uid=".$id)->order('stime desc')->select();
            //var_dump(M("withdraw")->getlastsql());die;
            $this->ajaxReturn($data);      
        }*/else
        {
            $this->display('Login/login');
        }
        
    }

    public function down(){
        if(IS_AJAX){
            $id=I('id');
            //var_dump($id);die;
            $data = M("withdraw")->join('register on withdraw.uid=register.id')->where("uid=".$id)->order('stime desc')->select();
            //var_dump(M("withdraw")->getlastsql());die;
            $this->ajaxReturn($data);       
        }
    }
    //上拉加载
    public function up(){
        if(IS_AJAX){
            $id=$_POST['id'];
            $index = I('index');
            $size = I('size');
            $start = ($index-1)*$size;
            $uer=M('withdraw')->join('register on withdraw.uid=register.id')->limit($start,$size)->where("uid=".$id)->order('stime desc')->select();
            $su=M('withdraw')->where("uid=".$id)->count();
            //var_dump($uer);
            $data=array(
                "count"=>$su,
                'index' => $index,
                'start' => $start,
                "info"=>$uer
            );
            $this->ajaxReturn($data);
                
        }
    }

    public function upload(){
        
        $upload = new Upload();
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './upload/'; // 设置附件上传根目录
        $upload->savePath  =      ''; // 设置附件上传（子）目录
        // 上传单个文件 
        $info   =   $upload->upload();
        //var_dump($info);die;
        //echo json_encode($info);
        if(!$info) {// 上传错误提示错误信息
            $this->ajaxReturn( $upload->getError());
        }else{// 上传成功 获取上传文件信息
             //echo $info['savepath'].$info['savename'];
            $big_img="./upload/".$info['file']['savepath'].$info['file']['savename'];
            $small_img="./upload/".$info['file']['savepath']."small_".$info['file']['savename'];
            //var_dump($img);die;
            $img = new \Think\Image();   
            $img->open($big_img);  
            $img->thumb(40,40);
            $img->save($small_img);
            $id=$_SESSION['msg']['id'];
            $data['headimg']="/upload/".$info['file']['savepath']."small_".$info['file']['savename'];;
            M('register')->where('id='.$id)->save($data);
            $this->ajaxReturn($small_img);
        }
        
    }
    public function modifyAvatar()
    {
        if (IS_AJAX)
        {
            // 头像地址
            $avatar = trim(I('avatar'));
            $id=$_SESSION['msg']['id'];
            $data['headimg']=$avatar;
            M('register')->where('id='.$id)->save($data);

            $this->ajaxReturn(1);
        }
    }
    //退出当前账号
    public function logout()
    {
        //echo 123;die;
        unset($_SESSION['msg']);
        $this->redirect("/index.php/home/Index/index");
    }

}